from CustomGuiItems.MyDockWidget import MyDockWidget;
from MovieScene.MovieScene_Canvas import MovieScene_Canvas;
from PyQt4 import QtGui, QtCore;
import pandas as pd;
import numpy as np;  # @UnusedImport
import re, os;

class MovieScene_Pane(QtCore.QObject):
    def __init__(self, parent):
        super(MovieScene_Pane, self).__init__();
        self.parent = parent;
        self.gSceneView = parent.graphScene.graphView;
        self.dataModel = parent.dataContainer.dataModel;
        self.mainWin = parent.mainWin;
        self.myDockWidget = MyDockWidget("Movie Scene View", self.parent);
        self.mainWin.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.myDockWidget);
        self.createDockContent();
        self.myDockWidget.setWidget(self.qWidget);
                
    def createDockContent(self):
        self.nDelay, self.nFrames = 40, 0;
        self.bShowGraph = False;
        #GUI Components
        self.canvasView = MovieScene_Canvas(self.dataModel, self.gSceneView);
        #Labels
        self.lblDelay = QtGui.QLabel("Delay:");
        self.lblFrame = QtGui.QLabel("Frame:");
        #Buttons
        self.btnPlay = QtGui.QPushButton("Play");
        self.btnStop = QtGui.QPushButton("Stop");
#         self.btnPrev = QtGui.QPushButton("Prev");
#         self.btnNext = QtGui.QPushButton("Next");
        self.btnZoom = QtGui.QPushButton("Zoom");
        self.btnLoad = QtGui.QPushButton("Load...");
        self.btnTracks = QtGui.QPushButton("Tracks...");
        self.btnPlay.setMaximumWidth(40);
        self.btnStop.setMaximumWidth(40);
#         self.btnPrev.setMaximumWidth(40);
#         self.btnNext.setMaximumWidth(40);
        self.btnZoom.setMaximumWidth(40);
        self.btnLoad.setMaximumWidth(50);
        self.btnTracks.setMaximumWidth(60);
        self.btnPlay.clicked.connect(self.onBtnPlay);
        self.btnStop.clicked.connect(self.onBtnStop);
#         self.btnPrev.clicked.connect(self.onBtnPrev);
#         self.btnNext.clicked.connect(self.onBtnNext);
        self.btnZoom.clicked.connect(self.onBtnZoom);
        self.btnLoad.clicked.connect(self.onBtnLoad);
        self.btnTracks.clicked.connect(self.onBtnTracks);
        #SpinBoxes
        self.spbxStpDelay = QtGui.QSpinBox(self.mainWin); 
        self.spbxStpDelay.setMaximum(60000);
        self.spbxStpDelay.setSingleStep(1);
        self.spbxStpDelay.setValue(self.nDelay);
        self.spbxStpDelay.valueChanged.connect(self.onStpDelayChanged);
        self.spbxFrame = QtGui.QSpinBox(self.mainWin); 
        self.spbxFrame.setMaximum(self.nFrames);
        self.spbxFrame.setSingleStep(1);
        self.spbxFrame.setValue(0);
        self.spbxFrame.valueChanged.connect(self.onFrameChanged);
        #CheckBoxes
        self.chkShowGraph = QtGui.QCheckBox("Show Graph", self.mainWin);
        self.chkShowGraph.toggled.connect(self.onChkShowGraph);
        self.chkShowGrpActs = QtGui.QCheckBox("Show Grps", self.mainWin);
        self.chkShowGrpActs.setChecked(True);
        self.chkShowGrpActs.toggled.connect(self.onChkShowGrpActs);
        #Timer for Animation
        self.timer = QtCore.QTimer();
        self.timer.setInterval(self.nDelay);
        self.timer.timeout.connect(self.onTimerUpdate);
        #Slider
        self.slider = QtGui.QSlider(QtCore.Qt.Horizontal);
        self.slider.setMaximum(self.nFrames);
        self.slider.valueChanged.connect(self.onChangeValue);
        #GridLayout
        self.gridLayout = QtGui.QGridLayout();
        self.gridLayout.addWidget(self.canvasView, 0,0, 1,11);
        self.gridLayout.addWidget(self.slider, 1,0, 1,11);
        self.gridLayout.addWidget(self.btnPlay, 2,0, 1,1);
        self.gridLayout.addWidget(self.btnStop, 2,1, 1,1);
#         self.gridLayout.addWidget(self.btnPrev, 2,2, 1,1);
#         self.gridLayout.addWidget(self.btnNext, 2,3, 1,1);
        self.gridLayout.addWidget(self.lblFrame, 2,2, 1,1, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.spbxFrame, 2,3, 1,1);
        self.gridLayout.addWidget(self.lblDelay, 2,4, 1,1, QtCore.Qt.AlignRight);
        self.gridLayout.addWidget(self.spbxStpDelay, 2,5, 1,1);
        self.gridLayout.addWidget(self.btnZoom, 2,6, 1,1);
        self.gridLayout.addWidget(self.btnLoad, 2,7, 1,1);
        self.gridLayout.addWidget(self.btnTracks, 2,8, 1,1);
        self.gridLayout.addWidget(self.chkShowGrpActs, 2,9, 1,1);
        self.gridLayout.addWidget(self.chkShowGraph, 2,10, 1,1);
        self.canvasView.createScene();
        self.qWidget = QtGui.QWidget(self.mainWin);
        self.qWidget.setLayout(self.gridLayout);
        
    def onChkShowGrpActs(self, bVal):
        self.canvasView.showGrpActs(bVal);
        
    def onChkShowGraph(self, bVal):
        self.bShowGraph = bVal;        
        if bVal:
            nFrm = self.slider.value();
            self.gSceneView.setNodes_CurrentStyle(nFrm);
        else:
            self.gSceneView.setNodes_NormalStyle();                    
    
    def onBtnZoom(self):
        if self.canvasView.gPixmapItem==None: return;
        if self.btnZoom.text()=="Zoom":
            qRect = self.canvasView.gPixmapItem.boundingRect();
            self.canvasView.fitInView(qRect, mode=QtCore.Qt.KeepAspectRatio);
            self.btnZoom.setText("Reset");
        else:
            self.canvasView.resetTransform();
            self.btnZoom.setText("Zoom");
        
    def onFrameChanged(self, nVal):
        self.slider.setValue(nVal);
        
    def onStpDelayChanged(self, nVal):
        if nVal==0: return;
        self.nDelay = nVal;
        self.timer.setInterval(self.nDelay);
        
    def onTimerUpdate(self):
        if self.nDelay==0: return;
        self.slider.setValue(self.slider.value()+1);
        
    def onBtnPlay(self):
        if self.canvasView.gPixmapItem==None: return;
        if self.btnPlay.text()=="Play":
            self.timer.start();
            self.btnPlay.setText("Pause");
        else:
            self.timer.stop();
            self.btnPlay.setText("Play");
        
    def onBtnStop(self):
        self.timer.stop();
        self.btnPlay.setText("Play");
        self.slider.setValue(0);
        
#     def onBtnPrev(self):
#         self.timer.stop();
#         self.btnPlay.setText("Play");
#         self.slider.setValue(self.slider.value()-1);
#         
#     def onBtnNext(self):
#         self.timer.stop();
#         self.btnPlay.setText("Play");
#         self.slider.setValue(self.slider.value()+1);
    
    def onChangeValue(self, value):
        self.canvasView.setFrame(value);
        self.spbxFrame.setValue(value);
        if self.bShowGraph:
            self.gSceneView.setNodes_CurrentStyle(value);
            
    def onBtnTracks(self):
        sTitle = "Load Tracks...";
        sDir = "C:/Users/Alfredo/workspace/Visual_B3/src/StoredData/Tracks/";
        sTracksFile = QtGui.QFileDialog.getOpenFileName(self.mainWin, sTitle, sDir);
        if sTracksFile == "": return;
        sTracksFile = str(sTracksFile.replace("\\", "/"));
        dfTracks = pd.read_csv(sTracksFile);
        dfTracks = dfTracks[dfTracks["ObjType"]==1]; #dfTracks["ObjType"]=="Person"
        self.canvasView.create_Track_Items(dfTracks);
        
    def onBtnLoad(self):
        sTitle = "Load File Frames...";
        sDir = 'C:/Users/Alfredo/Desktop/SISTA Docs & Notes/VIRAT_Videos/';
        self.sFilePath = QtGui.QFileDialog.getExistingDirectory(self.mainWin, sTitle, sDir);
        if self.sFilePath == "": return;
        self.sFilePath = str(self.sFilePath.replace("\\", "/"));
        aNames = self.getFileName(self.sFilePath);
        if aNames==None: return;        
        self.canvasView.loadFrames(aNames);
        #Setup Frames Loaded (in SpnBox & Slider)
        self.slider.setValue(0);
        self.slider.setMaximum(self.nFrames);
        nPgStep = self.nFrames*0.05;
        nPgStep = 1 if nPgStep<1 else nPgStep;
        self.slider.setPageStep(nPgStep);
        self.spbxFrame.setMaximum(self.nFrames);
        
    def getFileName(self, sPath):
        sFileName = "";
        self.nFrames = len(os.listdir(sPath))-2; #Ignoring the "Thumbs.db"
        for sFile in os.listdir(sPath):
            nZeros, nFile = -1, -1;
            reObj = re.match(r'.*\D+(\d+)\.|(\d+)\.', sFile);
            if reObj!=None:
                sNums = reObj.group(1) if reObj.group(1)!=None else reObj.group(2);
                nZeros = len(sNums);
                nFile = int(sNums);
            if nZeros>0 and nFile==0:
                sFileName = sFile.split(".")[0]+".";
                sFileExt = sFile.split(".")[1];
                sFileName = sFileName.replace(sNums+".", "");
                return [sPath, sFileName, nZeros, sFileExt];
        print "No Frame Files found."
        return None;
        